package org.jeecg.modules.service.impl;

import com.yaorange.jxc.goods.entity.Goods;
import org.jeecg.modules.pojo.User;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yaorange.jxc.supplier.entity.Supplier;
import com.yaorange.jxc.supplier.mapper.SupplierMapper;
import org.jeecg.modules.api.vo.PageVo;
import org.jeecg.modules.mapper.PurchaseGoodsMapper;
import org.jeecg.modules.mapper.PurchaseMapper;
import org.jeecg.modules.pojo.Purchase;
import org.jeecg.modules.pojo.PurchaseGoods;
import org.jeecg.modules.service.PurchaseService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @description:
 * @company: yaorange
 * @author: Andy.Li
 * @Date: 2022.04.26 16:00:35
 */
@Service
public class PurchaseServiceImpl implements PurchaseService {
    @Resource
    private SupplierMapper supplierMapper;
    @Resource
    private PurchaseMapper purchaseMapper;
    @Resource
    private PurchaseGoodsMapper purchaseGoodsMapper;


    @Override
    public List<Purchase> getList() {


        return purchaseMapper.getPurchaseAndUserAndSupplier();
    }

    @Override
    public PageVo<Purchase> getPage(Page<Purchase> page, String purchaseNumber, Integer supplierId, Integer state, String purchaseDate) {
        IPage<Purchase> iPage = purchaseMapper.page(page, purchaseNumber, supplierId, state, purchaseDate);
        PageVo<Purchase> pageVo = new PageVo<>();
        pageVo.setPages(iPage.getPages());
        pageVo.setList(iPage.getRecords());
        pageVo.setTotal(iPage.getTotal());
        return pageVo;
    }

    @Override
    @Transactional
    public int delById(Integer purchaseListId) {
        //查询所有的PurchaseGoods
        QueryWrapper<PurchaseGoods> wrapper = new QueryWrapper<>();
        wrapper.eq("purchase_list_id", purchaseListId);
        List<PurchaseGoods> goods = purchaseGoodsMapper.selectList(wrapper);
        //如果存在对应的purchasegoods则删除
        if (goods.size() > 0) {
            purchaseGoodsMapper.deleteByPurchaseId(purchaseListId);
        }
        //如果不存在直接删除
        return purchaseMapper.deleteById(purchaseListId);
    }

    @Override
    @Transactional
    public int addPurAndPurGoods(Purchase purchase) {
        QueryWrapper<Purchase> wrapper = new QueryWrapper<>();
        wrapper.eq("purchase_number", purchase.getPurchaseNumber());
        List<Purchase> list = purchaseMapper.selectList(wrapper);
        if (list.size()==0) {
            purchaseMapper.insert(purchase);
            return purchase.getPurchaseListId();
        }
        return list.get(0).getPurchaseListId();

    }

    @Override
    @Transactional
    public int addPurGoods(Goods goods) {
        PurchaseGoods purchaseGoods = new PurchaseGoods();
        purchaseGoods.setGoodsId(goods.getGoodsId());
        purchaseGoods.setGoodsCode(goods.getGoodsCode());
        purchaseGoods.setGoodsName(goods.getGoodsName());
        purchaseGoods.setGoodsModel(goods.getGoodsModel());
        purchaseGoods.setGoodsUnit(goods.getGoodsUnit());
        purchaseGoods.setGoodsNum(goods.getGoodsNum());
        purchaseGoods.setPrice(goods.getPurchasingPrice());
        purchaseGoods.setTotal(purchaseGoods.getPrice() * purchaseGoods.getGoodsNum());

        purchaseGoods.setGoodsTypeId(goods.getGoodsTypeId());
        //查询数据库的若没有PurchaseList
        QueryWrapper<Purchase> wrapper = new QueryWrapper<>();
        wrapper.eq("purchase_number", goods.getPurchaseNumber());
        List<Purchase> list = purchaseMapper.selectList(wrapper);
        //若没有
        if (list.size()==0) {
            return 0;
        }
        //若有则插入
        purchaseGoods.setPurchaseListId(list.get(0).getPurchaseListId());
        return purchaseGoodsMapper.insert(purchaseGoods);
    }


    public List<Supplier> getSupplierList() {
        return supplierMapper.selectList(new QueryWrapper<>());
    }
}
